package com.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.to.UpSkuTo;
import com.common.utils.PageUtils;
import com.common.utils.Query;
import com.gulimall.product.dao.ProductAttrValueDao;
import com.gulimall.product.entity.AttrEntity;
import com.gulimall.product.entity.ProductAttrValueEntity;
import com.gulimall.product.service.AttrService;
import com.gulimall.product.service.ProductAttrValueService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {


    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public List<ProductAttrValueEntity> getListforSpu(Long spuId) {
        List<ProductAttrValueEntity> spu_id = baseMapper.selectList(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
        return spu_id;
    }

    @Override
    public void updateAttrSpu(Long spuId, List<ProductAttrValueEntity> list) {
        this.remove(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id",spuId));
        List<ProductAttrValueEntity> collect = list.stream().map(productAttrValueEntity -> {
            productAttrValueEntity.setSpuId(spuId);
            return productAttrValueEntity;
        }).collect(Collectors.toList());
        this.saveBatch(collect);
    }

    @Override
    public List<UpSkuTo.Attrs> getEsAttr(Long spuId) {
        List<ProductAttrValueEntity> listforSpu = getListforSpu(spuId);


        List<Long> attrIdList = listforSpu.stream().map(productAttrValueEntity -> {
            return productAttrValueEntity.getAttrId();
        }).collect(Collectors.toList());

        //去重
        Set<Long> set=new HashSet<>(attrIdList);
        attrIdList=new ArrayList<>(set);

        List<UpSkuTo.Attrs> attrsList = attrIdList.stream().filter(attrId -> {
            return attrService.getById(attrId).getSearchType() == 1;
        }).map(attrId -> {
            AttrEntity attrEntity = attrService.getById(attrId);
            UpSkuTo.Attrs attrs = new UpSkuTo.Attrs();
            BeanUtils.copyProperties(attrEntity, attrs);
            attrs.setAttrValue(getAttrValue(listforSpu,attrId));
            return attrs;
        }).collect(Collectors.toList());

        return attrsList;
    }
    public static String getAttrValue(List<ProductAttrValueEntity> list,Long attrId){
        for (ProductAttrValueEntity productAttrValueEntity : list) {
            if(productAttrValueEntity.getAttrId()==attrId){
                return productAttrValueEntity.getAttrValue();
            }
        }
        return null;
    }
}